package com.hjqjl.ftp;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.PathUtils;

import org.apache.commons.net.ftp.FTPClient;

public class MainActivity extends AppCompatActivity {
    private final static String CFGPath = PathUtils.getExternalAppDownloadPath() + "/CFG";//酒精测试配置存储的位置
    private static final String ip = "xx";
    private static final int port = 0;
    private static final String userName = "xx";
    private static final String password = "xx";
    private static final String ftpDir = "/ad_update/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = findViewById(R.id.btn_down);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        updateCFG();
                    }
                }).start();
            }
        });
    }

    /**
     * 下载json文件(内有版本信息)
     */
    private void updateCFG() {
        String fileName = "CFG.json";
        try {
            FtpUtils ftpUtils = new FtpUtils();
            FTPClient ftpClient = ftpUtils.getFTPClient(ip, port, userName, password);

            if (ftpClient != null) {
                boolean isDown = ftpUtils.downLoadFTP(ftpClient, ftpDir, fileName, CFGPath);
                ftpUtils.closeFTP(ftpClient);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}